/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.umlutils.ICustomAppearence;

/**
 * Relation end edit part specialized in multiplicity display.
 * 
 * @author Remi Schnekenburger
 */
public class RelationEndMultiplicityDisplayEditPart extends	RelationEndDisplayEditPart {

	/**
	 * Creates a new RelationEndMultiplicityDisplayEditPart.
	 * 
	 * @param modelManager the model manager associated to this editor
	 * @param graphnode the parent graph node
	 */
	public RelationEndMultiplicityDisplayEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);
	}


	/**
	 * Creates a new RelationEndMultiplicityDisplayEditPart.
	 * 
	 * @param element the association "parent" of this relation end
	 * @param modelManager the model manager associated to this editor
	 * @param end boolean taht indicates if it is near the source  (<code>false</code>)or the target (<code>true</code>)
	 * @param graphnode the parent graph node
	 */
	public RelationEndMultiplicityDisplayEditPart(GraphNode graphnode, Association element, boolean end, ModelManager modelManager) {
		super(graphnode, element, end, true, modelManager);
	}

	/**
	 * Retrieves the property.
	 * 
	 * @return 
	 */
	protected Property getProperty() {
		if( element.getMemberEnds().size()>1){
			if (isAtEnd()){
				return (Property) element.getMemberEnds().get(0);
			}
			else {
				return (Property) element.getMemberEnds().get(1);
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	protected StringFigure createFigure() {
		final com.cea.papyrus.umlutils.Property utilProperty = new com.cea.papyrus.umlutils.Property(getProperty());

		// retrieve string corresponding to the multiplicity
		final String multiplicity = getText(utilProperty);
		return new StringFigure(multiplicity);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.RelationEndDisplayEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		final StringFigure f = (StringFigure) getFigure();
		if(getProperty()!=null){

			// retrieve associated property
			com.cea.papyrus.umlutils.Property utilProperty = new com.cea.papyrus.umlutils.Property(getProperty());

			// retrieve string corresponding to its multiplicity
			String multiplicity = getText(utilProperty);
			f.setText(multiplicity);

			f.setVisible(this.isVisible());
		}
	}

	/**
	 * Returns the text that should be displayed in the label for the given
	 * property.
	 * @param utilProperty the property to display label
	 * @return the text to be displayed
	 */
	private String getText(com.cea.papyrus.umlutils.Property utilProperty) {
		return utilProperty.getCustomLabel(ICustomAppearence.DISP_MULTIPLICITY);
	}
}
